package UILayer;

import ControllerLayer.*;
import ModelLayer.*;
/**
 * Class responsible for the EmployeeUI.
 * 
 * @author Anders A.P., Mie P., Anna K., Jakob K., Danny P.
 * @version 1.0 
 */
public class EmployeeUI
{
    private EmployeeCtr employeeCtr;
    private InputDialog inputDialog;
    /**
     * Constructor for objects of class EmployeeUI
     */
    public EmployeeUI()
    {
        employeeCtr = new EmployeeCtr();
        inputDialog = new InputDialog();

    }

    /**
     * Method which initiates the UI for handling the details of a customer.
     */
    public void startEmployeeUI()
    {
        boolean goOn = true;
        MenuList<Integer> m1 = new MenuList<Integer>("\fMedarbejder menu:", "\nVælg: ");
        m1.addOption("1", "Opret medarbejder", 1);
        m1.addOption("2", "Print medarbejderinfo", 2);
        m1.addOption("3", "Opdater medarbejderinformation", 3);
        m1.addOption("4", "Slet medarbejder", 4);
        m1.addOption("0", "Exit", 0);

        while(goOn)
        {
            Integer choice = m1.display();
            switch(choice){
                case 0: 
                    goOn = false;
                    break;
                case 1:
                    createEmployee();
                    break;
                case 2:
                	retrieveEmployee();
                    /*try {
                        retrieveEmployee();
                    }
                    catch (Exception e) { 
                        System.out.println("Der opstod en fejl ved hentning af medarbejderen.");
                    }*/
                    break;
                case 3:
                	updateEmployee();
                    /*try
                    {
                        updateEmployee();
                    }
                    catch (Exception e) 
                    {
                        System.out.println("Der opstod en fejl ved opdatering af medarbejderen.");
                    }*/
                    break;
                case 4:
                	deleteEmployee();
                    /*try 
                    {
                        deleteEmployee();
                    }
                    catch (Exception e) 
                    {
                        System.out.println("Der opstod en fejl ved sletning af medarbejderen.");
                    }*/
                    break;
                default: 
                    System.out.println("Input ikke genkendt");
                    break;
            }
        }
    }

    /**
     * Creates an employee using inputs from the user.
     */
    private void createEmployee()
    {
       try {
    	   employeeCtr.createEmployee(inputDialog.inputString("Medarbejderens navn: "),
                   inputDialog.inputString("Medarbejderens addresse: "),  
                   inputDialog.inputString("Medarbejderens e-mail: "),
                   inputDialog.inputString("Medarbejderens telefonnummer: "));
       } 
       catch (Exception e){
    	   System.out.println(e);
       }
    }

    /**
     * Retrieves an Employee object to the UI layer, allowing the user to interact with the object.
     */
    private void retrieveEmployee() 
    {
        try {
        	Employee e = employeeCtr.getEmployee(inputDialog.inputInt("Medarbejderens ID: "));
            System.out.printf("Medarbejderens ID:\t\t%d\n", e.getEmployeeNo());
            System.out.printf("Medarbejderens navn:\t\t%s\n", e.getName());
            System.out.printf("Medarbejderens addresse:\t%s\n", e.getAddress());
            System.out.printf("Medarbejderens e-mail:\t\t%s\n", e.getEmail());
            System.out.printf("Medarbejderens tlf:\t\t%s\n", e.getTlf());
            inputDialog.inputWait();
        }
        catch (Exception e){
        	System.out.println(e);
        	inputDialog.inputWait();
        }
    }

    /**
     * Retrieves and updates the information in an Employee object.
     */
    private void updateEmployee()
    {
    	try{
    		int id     = inputDialog.inputInt("Medarbejderens ID: ");
            Employee e = employeeCtr.getEmployee(id); 
            employeeCtr.updateEmployee(id,
                                       inputDialog.updateString("navn", e.getName()),
                                       inputDialog.updateString("addresse", e.getAddress()),
                                       inputDialog.updateString("e-mail", e.getEmail()),
                                       inputDialog.updateString("telefon", e.getTlf()));
            inputDialog.inputWait();
    	}
    	catch (Exception e) {
    		System.out.println(e);
    		inputDialog.inputWait();
    	}
                
    }

    /**
     * Deletes a Customer object from the collection.
     */
    private void deleteEmployee() 
    {
    	try {
    		employeeCtr.deleteEmployee(inputDialog.inputInt("Medarbejderens ID: "));
            inputDialog.inputWait();    
    	}
    	catch (Exception e){
    		System.out.println(e);
    	}
        
    }

}


